package com.aranandroid.customview.dialog

import android.annotation.SuppressLint
import android.app.Activity
import android.widget.TextView
import android.widget.LinearLayout
import android.app.Dialog
import android.companion.BluetoothLeDeviceFilter
import android.content.Context
import android.os.Build
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.cardview.widget.CardView

import android.view.*
import android.view.WindowManager




class CustomDialog @SuppressLint("ResourceAsColor") constructor(builder: Builder) :
    Dialog(builder.context, R.style.UpdateErrorFinishDialog) {
    // 标题
    lateinit var tvtitle: TextView
    // 取消文字
    lateinit var cancel: TextView
    // 确认取消中间黑条
    lateinit var middle: View
    // 底部灰色横向
    lateinit var lineBottom: View
    // 确认文字
    lateinit var confirm: TextView
    // 内容布局
    var contentLayout: DialogLayout
    // 整体父布局
    lateinit var cardview: CardView
    // 底部按钮父布局
    lateinit var bottomLayout: LinearLayout
    val childView: View
        get() = contentLayout.inflate
    var config: Config = Config()
    lateinit var mContext: Context
    companion object {
        /**
         * tvtitle: 标题,
         * cancel: 取消文字,
         * middle: 确认取消中间黑条,
         * lineBottom: 底部灰色横向,
         * confirm: 确认文字,
         * dialogLayout: 内容父布局,
         * cardview: 整体父布局
         * bottomLayout: 底部按钮父布局
         */
        var theDefaultStyles: (
            title: TextView,
            cancel: TextView,
            confirm: TextView,
            config: Config,
            contentLayout: DialogLayout,
            bottomLayout: LinearLayout,
            middle: View,
            lineBottom: View
        ) -> Unit =
            { title, cancel, confirm, cardview, contentLayout, bottomLayout, middle, lineBottom -> }
    }


    private fun initView(builder: Builder) {
        mContext = builder.context
        childView.apply {
            tvtitle = findViewById(R.id.title)
            cancel = findViewById(R.id.cancel)
            middle = findViewById(R.id.middle)
            confirm = findViewById(R.id.confirm)
            bottomLayout = findViewById(R.id.ll1)
            cardview = findViewById(R.id.parentdialog)
            lineBottom = findViewById(R.id.line_bottom)
        }
        // 设置回调：1默认回调 2临时回调
        // 1默认回调（静态）
        theDefaultStyles(
            tvtitle,
            cancel,
            confirm,
            config,
            contentLayout,
            bottomLayout,
            middle,
            lineBottom
        )
        // 2临时回调
        builder.tempStyles(
            tvtitle,
            cancel,
            confirm,
            config,
            contentLayout,
            bottomLayout,
            middle,
            lineBottom
        )
        cardview.radius = config.radius
        tvtitle.text = builder.title
        confirm.text = builder.confirm
        cancel.text = builder.cancle
        if (builder.confirmnoly) {
            cancel.visibility = View.GONE
            middle.visibility = View.GONE
        }
        if (!builder.btShow) {
            bottomLayout.visibility = View.GONE
            lineBottom.visibility = View.GONE
        }
        tvtitle.setOnTouchListener { v: View?, event: MotionEvent? -> true }
        // 设置按钮监听
        cancel.setOnClickListener {
            builder.cancelListenter(childView, this)
            if(builder.cancelListenterConDis(childView, this))
            dismiss()
        }
        confirm.setOnClickListener {
            builder.confirmListenter(childView, this)
            if(builder.confirmListenterConDis(childView, this))
            dismiss()
        }
    }


    class Config{
        // 屏幕中高度的位置;-1底部1头上
        var heightLoction = 0.1
        // 宽度比例
        var widthProportion = 0.9
        // 圆角
        var radius = 20f
    }

    class Builder(val context: Context) {
        var dialogLayoutId = 0
        var title = "提示"
        var cancle = "取消"
        var confirm = "确认"
        // 是否有按钮
        var btShow = true

        // 只有一个确认按钮
        var confirmnoly = false

        // 取消事件
        var cancelListenter: (view: View, dialog: CustomDialog) -> Unit = { view, dialog -> }
        // 取消事件 可控制dialog是否消失
        var cancelListenterConDis: (view: View, dialog: CustomDialog) -> Boolean = { view, dialog -> true}

        // 确认事件
        var confirmListenter: (view: View, dialog: CustomDialog) -> Unit = { view, dialog -> }
        // 确认事件 可控制dialog是否消失
        var confirmListenterConDis: (view: View, dialog: CustomDialog) -> Boolean = { view, dialog -> true}

        /**
         * tvtitle: 标题,
         * cancel: 取消文字,
         * middle: 确认取消中间黑条,
         * confirm: 确认文字,
         * lineBottom: 底部灰色横向,
         * dialogLayout: 内容父布局,
         * cardview: 整体父布局
         * bottomLayout: 底部按钮父布局
         */
        var tempStyles: (
            title: TextView,
            cancel: TextView,
            confirm: TextView,
            config: Config,
            contentLayout: DialogLayout,
            bottomLayout: LinearLayout,
            middle: View,
            lineBottom: View
        ) -> Unit =
            { title, cancel, confirm, cardview, contentLayout, bottomLayout, middle, lineBottom -> }

        fun tempStyles(
            tempStyles: (
                title: TextView,
                cancel: TextView,
                confirm: TextView,
                config: Config,
                contentLayout: DialogLayout,
                bottomLayout: LinearLayout,
                middle: View,
                lineBottom: View
            ) -> Unit
        ): Builder {
            this.tempStyles = tempStyles
            return this
        }


        fun cancelListenter(
            cancelListenter: (
                view: View,
                dialog: CustomDialog
            ) -> Unit
        ): Builder {
            this.cancelListenter = cancelListenter
            return this
        }

        fun confirmListenter(
            confirmListenter: (
                view: View,
                dialog: CustomDialog
            ) -> Unit
        ): Builder {
            this.confirmListenter = confirmListenter
            return this
        }

        fun cancelListenterConDis(
            cancelListenterConDis: (
                view: View,
                dialog: CustomDialog
            ) -> Boolean
        ): Builder {
            this.cancelListenterConDis = cancelListenterConDis
            return this
        }

        fun confirmListenterConDis(
            confirmListenterConDis: (
                view: View,
                dialog: CustomDialog
            ) -> Boolean
        ): Builder {
            this.confirmListenterConDis = confirmListenterConDis
            return this
        }

        fun title(title: String): Builder {
            this.title = title
            return this
        }

        fun cancle(cancle: String): Builder {
            this.cancle = cancle
            return this
        }

        fun confirm(confirm: String): Builder {
            this.confirm = confirm
            return this
        }

        fun btShow(btShow: Boolean): Builder {
            this.btShow = btShow
            return this
        }

        fun confirmnoly(confirmnoly: Boolean): Builder {
            this.confirmnoly = confirmnoly
            return this
        }


        fun dialogLayoutId(dialogLayoutId: Int): Builder {
            this.dialogLayoutId = dialogLayoutId
            return this
        }

        fun build(): CustomDialog {
            return CustomDialog(this)
        }


    }


    init {
        contentLayout =
            DialogLayout(builder.context, builder.dialogLayoutId)
        addContentView(
            contentLayout,
            ConstraintLayout.LayoutParams(
                ConstraintLayout.LayoutParams.MATCH_PARENT,
                ConstraintLayout.LayoutParams.MATCH_PARENT
            )
        )
        initView(builder)
    }


    override fun show() {
        super.show()
        val window: Window? = getWindow()
        window?.apply {
            val d = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                context.display
            } else {
                (mContext as Activity).windowManager.defaultDisplay
            } // 获取屏幕宽、高用
            val p = attributes // 获取对话框当前的参数值
            d?.also {
                p.y = -(d.height * config.heightLoction / 2).toInt() // 改变的是dialog框在屏幕中的位置而不是大小
                p.width = (d.width * config.widthProportion).toInt() // 宽度设置为屏幕的0.65
                attributes = p
            }

        }

    }
}